<template>
  <div>
    <h1>车位列表</h1>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="id" width="60" align="center"></el-table-column>
      <el-table-column prop="carId" label="车位编号"></el-table-column>
      <el-table-column prop="name" label="车位名称"></el-table-column>
      <el-table-column prop="address" label="车位地址"></el-table-column>
      <el-table-column prop="gmtCreate" label="创建时间"></el-table-column>
      <el-table-column prop="gmtModified" label="最后修改时间"></el-table-column>
      <el-table-column prop="enable" label="状态">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="handleChangeEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
    }
  },
  methods: {
    openDeleteConfirm(rows) {
      this.$confirm('即将永久删除【' + rows.name + '】品牌, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(rows.id);
      }).catch(() => {
      });
    },
    handleDelete(id) {
      console.log('准备【删除品牌】，数据id为【' + id + '】……');
      let url = 'http://localhost:8081/pms/' + id + '/delete';
      console.log('url = ' + url);
      this.axios
          .post(url)
          .then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              console.log('删除成功！');
              this.$message({
                message: '删除成功！',
                type: 'success'
              });
            } else if (responseBody.state == 40400) {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
            } else {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
              return;
            }
            this.loadAdminList();
          })
    },
    loadAdminList() {
      console.log('准备从服务器端加载【管理员列表】……');
      let url = 'http://localhost:8081/pms';
      console.log('请求路径：' + url);
      this.axios
          .get(url)
          .then((response) => {
            let responseBody = response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseBody);
            this.tableData = responseBody.data;
          });
    },
  },
  mounted() {
    console.log('mounted');
    this.loadAdminList();
  }
}
</script>

<style scoped>

</style>